define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'video/index' + location.search,
                    add_url: 'video/add',
                    edit_url: 'video/edit',
                    del_url: 'video/del',
                    multi_url: 'video/multi',
                    import_url: 'video/import',
                    table: 'video',
                }
            });

            var table = $("#table");

            table.on('post-common-search.bs.table', function (event, table) {
                // 表单
                var form = $("form", table.$commonsearch);

                // 设置绑定合集
                $("input[name='collection_id']", form).addClass("selectpage").data("source", "videocollection/index").data("primaryKey", "id").data("field", "name").data("orderBy", "id desc").data("pageSize", 10);

                // 注册事件
                Form.events.selectpage(form);
            });


            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                searchFormVisible: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('视频编号')},
                        {field: 'name', title: __('视频名称'), operate: 'LIKE'},
                        {field: 'status', title: __('上架状态'), formatter: Table.api.formatter.normal, searchList: {'1': __('是'), '2': __('否')}},
                        {field: 'is_free', title: __('是否免费'), formatter: Table.api.formatter.normal, searchList: { '1': __('是'), '0': __('否') } },
                        {field: 'collection.name', title: __('绑定合集')},
                        {field: 'collection_id', title: __('绑定合集'),visible: false},
                        {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
